package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.TempHumContract;
import com.sunlg.smarthome.data.entity.Humidity;
import com.sunlg.smarthome.data.entity.Temperature;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;

import java.util.Date;
import java.util.List;

/**
 * Created by sunlg on 2017/5/13.
 */

public class TempHumPresenter implements TempHumContract.Presenter {

    private TempHumContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;

    public TempHumPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void loadSectionTemp(int deviceId, Date start, Date end) {
        mSHServiceWrapper.getSectionTemp(deviceId, start, end, new MySubscriber<Result<List<Temperature>>>() {
            @Override
            public void onSuccess(Result<List<Temperature>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.showTempChart(data.getData());
                }else {
                    mView.hideTempChart();
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.hideTempChart();
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void loadSectionHum(int deviceId, Date start, Date end) {
        mSHServiceWrapper.getSectionHum(deviceId, start, end, new MySubscriber<Result<List<Humidity>>>() {
            @Override
            public void onSuccess(Result<List<Humidity>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.showHumChart(data.getData());
                }else {
                    mView.hideHumChart();
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.hideHumChart();
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(TempHumContract.View view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }
}
